package com.qy127.controller;

import com.qy127.dao.StaffDao;
import com.qy127.entity.Staff;
import com.qy127.util.OSSUploadUtil;
import com.qy127.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/staff")
public class StarffController {

    /**
     * 注入StaffDao
     */
    @Autowired
    private StaffDao staffDao;



    /**
     * 登录
     * @param staffId
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("/login")
    public Map login(String staffId, String password, HttpServletRequest request){
        //数据操作
        //System.out.println(staffId+"aaaaaa");
        Staff staff = staffDao.getStaffByStaffIdAndPassword(staffId, password);
        HashMap<Object, Object> map = new HashMap<>();
        if (staff==null||"".equals(staff)){
            map.put("code",300);
            map.put("msg","该用户不存在");
        }else {
            //先将staff存储到session中
            request.getSession().setAttribute("staff",staff);
            map.put("code",200);
        }
        return map;
    }

    /**
     *  退出
     * @return
     */
    @RequestMapping("/exit")
    public ModelAndView exit(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("login");
        return mv;
    }

    /**
     * 查询所有员工信息
     * @param staff
     * @return
     */
    @RequestMapping("/showStaffs")
    @ResponseBody
    public Result staffList(Staff staff){
        //System.out.println(staff);
        List<Map<String, Object>> allStaff = staffDao.getAllStaff(staff);
        int i = staffDao.getAllCount(staff);
        Result result = new Result(i, allStaff);
        return result;
    }

    /**
     * 新增或修改员工
     * @param staff
     * @return
     */
    @RequestMapping("/insertAndUpdateStaff")
    public Map insertStaff(Staff staff){
        HashMap<Object, Object> map = new HashMap<>();
        String idCard = staff.getIdCard();
        List<Map<String, Object>> allIdCard = staffDao.getAllIdCard();

        int i =0;
        if (staff.getId()==null){
            for (Map<String, Object> m : allIdCard) {
                if (idCard.equals(m.get("idCard"))){
                    map.put("code","500");
                    map.put("msg","该身份证已存在");
                    return map;
                }
            }
             i = staffDao.insertStaff(staff);
            if (i>0){
                map.put("code","200");
                map.put("msg","新增成功");
            }else{
                map.put("code","400");
                map.put("msg","新增失败");
            }

        }else{
            i = staffDao.updateStaff(staff);
            if (i>0){
                map.put("code","200");
                map.put("msg","修改成功");
            }else{
                map.put("code","400");
                map.put("msg","修改失败");
            }
        }
        return map;
    }







    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    public Map updateStaffByStatus(Integer id,Integer status){
        int i = staffDao.updateStatus(id, status);
        HashMap<Object, Object> map = new HashMap<>();
        if (i>0){
            map.put("code",200);
            map.put("msg","修改成功");
        }
        return map;
    }



    /**
     * 员工上传头像
     * @param file
     * @param staff
     * @return
     */
    @RequestMapping("/upload")
    public @ResponseBody Map upload(MultipartFile file,Staff staff){
        String s = OSSUploadUtil.upload2(file);
        System.out.println(s);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("url",s);
        return map;
    }

    /**
     * 获取员工编号最大值+1
     * @return
     */
    @RequestMapping("/getMaxStaffId")
    public @ResponseBody int  getMaxStaffId(){
        int maxStaffId = staffDao.getMaxStaffId();
        return maxStaffId+1;
    }

}
